# Contributor: Teemu Nätkinniemi ("teepean") <tnatkinn@gmail.com>

_realname=htslib
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.18
pkgrel=1
pkgdesc="library for high-throughput sequencing data formats"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/samtools/htslib"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-libsystre"
         "${MINGW_PACKAGE_PREFIX}-libdeflate"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools")
source=("https://github.com/samtools/htslib/releases/download/$pkgver/$_realname-$pkgver.tar.bz2"
        0001-makefile-remove-msvc-lib.patch)
sha256sums=('f1ab53a593a2320a1bfadf4ef915dae784006c5b5c922c8a8174d7530a9af18f'
            '2e3c8eeff2b4b16a6d420243d69fbccde44d250ad6ef9ac647c9b113097b9dd6')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}

  # This removes commands which create MSVC like import libraries, See
  # https://github.com/samtools/htslib/commit/3f7e13e0688e50fca61e6f043e6655a165d596a2

  patch -p1 -i "${srcdir}/0001-makefile-remove-msvc-lib.patch"

  autoreconf -fvi
}

build() {
  # make check does not work with out-of-tree build
  cp -r "${_realname}-${pkgver}" "build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  ./configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --enable-libcurl \
    --disable-plugins

  make
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  make check || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  make DESTDIR=$pkgdir install

  # Rename import library with lib prefix
  mv -v "${pkgdir}${MINGW_PREFIX}/lib/hts.dll.a" "${pkgdir}${MINGW_PREFIX}/lib/libhts.dll.a"

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE $pkgdir${MINGW_PREFIX}/share/licenses/$_realname/LICENSE
}

